<?php
/**
 * Created by PhpStorm.
 * User: davin
 * Date: 14-10-19
 * Time: 下午12:03
 *
 * 食物类
 */

use LaravelBook\Ardent\Ardent;

class Food extends Ardent{

    public static $rules = array(
        'name' => 'required|min:2',
        'original_price' => 'regex:/^\d*(\.\d{2})?$/',
        'price' => 'required|regex:/^\d*(\.\d{2})?$/',
        'count' => 'numeric',
    );

    public static $relationsData = array(
        'menu' => array(self::BELONGS_TO, 'Menu', 'foreignKey' => 'menu_id', 'otherKey' => 'id'),
        'foods' => array(self::BELONGS_TO_MANY, 'Food', 'table' => 'package_foods', "foreignKey"=> 'package_id',"otherKey"=> 'food_id'),
        'thumbs' => array(self::MORPH_MANY, 'Thumb', 'name' => 'thumbable'),
    );

    public static $statuses = array(
        array('id' => 0,'keyword'=>'SELLING',   'display_name'=>'销售中'),
        array('id' => 1,'keyword'=>'STOP_SELL', 'display_name'=>'停止销售'),
        array('id' => 2,'keyword'=>'EMPTY',     'display_name'=>'售罄'),
        array('id' => 3,'keyword'=>'READY_SELL','display_name'=>'准备销售'),
    );

    const TYPE_PACKAGE = 'PACKAGE';

    public static $column = array(
        'menu_id' => '菜单',
        'name' => '餐品名称',
        'original_price' => '原价(单位:元,如:10.00)',
        'price' => '现价(单位:元,如:8.50)',
        'count' => '数量(单位:份)',
        'status' => '状态',
        'created_at' => '创建时间',
        'description' => '描述',
    );

    public static function boot()
    {
        parent::boot();

        self::saved(function($entry){

            $thumb_paths = Input::get( 'thumbs', '' );
            $entry->thumbs()->delete();
            if(is_array($thumb_paths)){
                foreach ($thumb_paths as $thumb_path){
                    $thumb = new Thumb();
                    $thumb->path = $thumb_path;
                    $entry->thumbs()->save($thumb);
                }
            }
        });
    }

    public static function getStatusDisplayNameByKeyword($keyword){
        foreach(self::$statuses as $status){
            if($status['keyword'] == $keyword){
                return $status['display_name'];
            }
        }
        return false;
    }

    public static function getCSVContent($entries){
        $output = '';
        foreach($entries as $result){
            foreach(array_keys(self::$column) as $column){
                switch($column){
                    case 'menu_name':
                        $output = $output.$result->menu->name.',';
                        break;
                    case 'status':
                        $output = $output.self::$statuses[$result->$column]['display_name'].',';
                        break;
                    default:
                        $output = $output.$result->$column.',';
                        break;
                }
            }
            $output = $output."\r\n";
        }

        return $output;
    }

    public static function getExcelContent($entries){
        $output = '<tbody>';
        foreach($entries as $result){
            $output = $output.'<tr>';
            foreach(array_keys(self::$column) as $column){
                switch($column){
                    case 'menu_name':
                        $output = $output.'<td>'.$result->menu->name.'</td>';
                        break;
                    case 'status':
                        $output = $output.'<td>'.self::$statuses[$result->$column]['display_name'].'</td>';
                        break;
                    default:
                        $output = $output.'<td>'.$result->$column.'</td>';
                        break;
                }
            }
            $output = $output.'</tr>';
        }

        return $output.'</tbody>';;
    }


    public function isPackage(){
        return $this->menu ? (strpos($this->menu->name, self::TYPE_PACKAGE) === false ? false : true) : false;
    }


    public function isInMyBasket(){
        return Basket::where('member_id', Auth::user()->attach->id)->where('food_id', $this->id)->count() >0;
    }




}